package com.xdf.wxbebarrel.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xdf.wxbebarrel.model.WechatMenuButton;
import com.xdf.wxbebarrel.model.WechatSubButton;
import com.xdf.wxbebarrel.model.WechatViewButton;
import com.xdf.wxbebarrel.model.WxAccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URLEncoder;

@Component
public class CreatMenu {
    @Autowired
    private WxAccessToken wxAccessToken;
    private static final ObjectMapper mapper = new ObjectMapper();

    public void create() throws IOException {
        WechatMenuButton button = new WechatMenuButton();
        // 顺杰货运 - 公司简介
        String companyProfile = "https://wxshunjie.barrel.fit/companycn.html";
        String companyProfileEncoded = URLEncoder.encode(companyProfile);
        String companyProfileFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + DevMaterial.JZY.APPID + "&redirect_uri=" + companyProfileEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 顺杰货运 - 报价单
        String quotation = "https://wxshunjie.barrel.fit/quotationcn.html";
        String quotationEncoded = URLEncoder.encode(quotation);
        String quotationFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + DevMaterial.JZY.APPID + "&redirect_uri=" + quotationEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 顺杰货运 - 航运跟踪
        String shippingtracking = "https://wxshunjie.barrel.fit/shippingtrackingcn.html";
        String shippingtrackingEncoded = URLEncoder.encode(shippingtracking);
        String shippingtrackingFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + DevMaterial.JZY.APPID + "&redirect_uri=" + shippingtrackingEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // 顺杰货运 - 报关单
        String customsdeclaration = "https://wxshunjie.barrel.fit/customsdeclarationcn.html";
        String customsdeclarationEncoded = URLEncoder.encode(customsdeclaration);
        String customsdeclarationFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + DevMaterial.JZY.APPID + "&redirect_uri=" + customsdeclarationEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";


        // shunjie - company profile
        String companyProfileEn = "https://wxshunjie.barrel.fit/companyen.html";
        String companyProfileEnEncoded = URLEncoder.encode(companyProfileEn);
        String companyProfileEnFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + DevMaterial.JZY.APPID + "&redirect_uri=" + companyProfileEnEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // shunjie - quotation
        String quotationEn = "https://wxshunjie.barrel.fit/quotationen.html";
        String quotationEnEncoded = URLEncoder.encode(quotationEn);
        String quotationEnFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + DevMaterial.JZY.APPID + "&redirect_uri=" + quotationEnEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // shunjie - shipping tracking
        String shippingtrackingEn = "https://wxshunjie.barrel.fit/shippingtrackingen.html";
        String shippingtrackingEnEncoded = URLEncoder.encode(shippingtrackingEn);
        String shippingtrackingEnFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + DevMaterial.JZY.APPID + "&redirect_uri=" + shippingtrackingEnEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";

        // shunjie - customs declaration
        String customsdeclarationEn = "https://wxshunjie.barrel.fit/customsdeclarationen.html";
        String customsdeclarationEnEncoded = URLEncoder.encode(customsdeclarationEn);
        String customsdeclarationEnFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
                DevMaterial.JZY.APPID +
                "&redirect_uri=" + customsdeclarationEnEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";


        // 关于
//        String admin = "https://wxshunjie.barrel.fit/admin.html?version=1.1";
//        String adminEncoded = URLEncoder.encode(admin);
//        String adminEncodedFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
//                DevMaterial.JZY.APPID +
//                "&redirect_uri=" + adminEncoded + "&response_type=code&scope=snsapi_base&state=1#wechat_redirect";
        String adminUrl = URLEncoder.encode("https://wxshunjie.barrel.fit/admin.html?version=1.3");
        String adminEncodedFull = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+DevMaterial.JZY.APPID
                +"&redirect_uri="+adminUrl+"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
//        String adminEncodedFull = WechatUtils.getWechatProxyUrl("https://wxshunjie.barrel.fit/admin.html?version=1.2");

        // 主菜单第一个按钮 - 没有子菜单
        WechatSubButton sb01 = new WechatSubButton("顺杰货运");
        sb01.getSub_button().add(new WechatViewButton("公司简介", companyProfileFull));
        sb01.getSub_button().add(new WechatViewButton("报价单", quotationFull));
        sb01.getSub_button().add(new WechatViewButton("航运跟踪", shippingtrackingFull));
        sb01.getSub_button().add(new WechatViewButton("报关单", customsdeclarationFull));
        button.getButton().add(sb01);

        // 主菜单第二个按钮
        WechatSubButton sb02 = new WechatSubButton("shunjie");
        sb02.getSub_button().add(new WechatViewButton("Company Profile", companyProfileEnFull));
        sb02.getSub_button().add(new WechatViewButton("quotation", quotationEnFull));
        sb02.getSub_button().add(new WechatViewButton("shipping tracking", shippingtrackingEnFull));
        sb02.getSub_button().add(new WechatViewButton("customs declaration", customsdeclarationEnFull));
        button.getButton().add(sb02);

        // 主菜单第三个按钮
        WechatSubButton sb03 = new WechatSubButton("关于");
        sb03.getSub_button().add(new WechatViewButton("管理员", adminEncodedFull));
        button.getButton().add(sb03);

        String btnString = mapper.writeValueAsString(button);

        String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN";
        String access_token = wxAccessToken.getAccessToken();
        url = url.replace("ACCESS_TOKEN", access_token);
        String result = HttpUtils.postForm(url, btnString,false);
        System.out.println("创建菜单的结果：" + result);
    }

}
